<?php

global $WSC;

require_once('tcms_vpt_page_wp.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/category/tcms_category_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/vp_templates/tcms_vpt_article_page_wp.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/vp_templates/tcms_vpt_news_page_wp.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_article_dbc.class.php');


class TCMS_VPT_News_Page_WP extends TCMS_VPT_Page_WP {
	function __construct(TCMS_VPage_DBC $vpage, TCMS_VPage_Template_DBC $vpt) {
		parent::__construct($vpage, $vpt, 'tcms_vpt_news_page');
	}


	protected function default_event() {
		$dbc = $this->get_dbc();
		$category = $this->load_vpage_category($dbc, $this->get_vpage()->get_id());
		if ($category) {
			$this->export('category', $category);

			$items_per_page_arr = TCMS_VPage_System_Attribute_DBC::load_all_by_hid_vpage($dbc, 'items_per_page', $this->get_vpage()->get_id());
			if ($items_per_page_arr) {
				$items_per_page_sa = current($items_per_page_arr);
				$items_per_page = $items_per_page_sa->get_value();
				if (!tangra_is_int($items_per_page)) {
					$items_per_page = 5;
				}
			} else {
				$items_per_page = 5; // TODO - may be this have to be on setting in registry?
			}

			$articles_arr = $this->select_vpages($dbc, $category->get_id(), 'TCMS_VPT_Article_Page_WP', $items_per_page);
			$this->prepare_for_export_articles($articles_arr);
			$this->export('articles_arr', $articles_arr);

			$subcat_arr = $this->select_subcat_vpages($dbc, $category->get_id(), 'TCMS_VPT_News_Page_WP', $items_per_page);
			$this->prepare_for_export_subcats($subcat_arr, $items_per_page);
			$this->export('subcat_arr', $subcat_arr);
		}

		$settings_images = TCMS_Settings_Images_DBC::load_settings_flat($this->get_dbc());
		$this->export('t1_width', $settings_images['thumbnail1_size_x']);
		$this->export('t1_height', $settings_images['thumbnail1_size_y']);
		$this->export('t2_width', $settings_images['thumbnail2_size_x']);
		$this->export('t2_height', $settings_images['thumbnail2_size_y']);
		
		return parent::default_event();
	}


	public static function get_short_text_description(DB_Connection $dbc, $vpage_id) {
		tangra_if_not_int_throw_e($vpage_id);
		$page_system_attributes = TCMS_VPage_System_Attribute_DBC::load_all_for_vpage($dbc, $vpage_id);

		$ret = '';

		if ($page_system_attributes) {
			$cat_hid = current($page_system_attributes)->get_value();
			$category = new TCMS_Category_DBC();
			if ($category->load_by_hid($dbc, $cat_hid)) {
				$ret = $category->get_title();
			}
		}

		return $ret;
	}


	private function prepare_for_export_articles(&$vpages_arr) {
		$dbc = $this->get_dbc();
		$hidden_inc = $this->_config->get_hidden_dir().'inc/';

		foreach($vpages_arr as $key => $vpage) {
			$article = TCMS_VPT_Article_Page_WP::get_article_ci_for_page($dbc, $vpage['vpage']);
			if ($article) {
				$vpages_arr[$key]['ci']['title'] = strip_tags($article->get_title()->get_value());
				$vpages_arr[$key]['ci']['intro'] = $article->get_intro()->get_value();
				$vpages_arr[$key]['ci']['images'] = TCMS_VPT_Page_WP::load_images_for_ci($dbc, $article->get_id());
				$vpages_arr[$key]['ci']['attachments'] = TCMS_VPT_Page_WP::load_attachments_for_ci($dbc, $article->get_id());
				$vpages_arr[$key]['ci']['created'] = $article->get_ci()->get_created();
			}
		}
	}


	private function prepare_for_export_subcats(&$vpages_arr, $items_per_page) {
		$dbc = $this->get_dbc();
		$hidden_inc = $this->_config->get_hidden_dir().'inc/';

		foreach($vpages_arr as $key => $vpage) {
			$vpages_arr[$key]['title'] = TCMS_VPT_News_Page_WP::get_short_text_description($dbc, $vpages_arr[$key]['vpage']);

			$category = $this->load_vpage_category($dbc, $vpages_arr[$key]['vpage']);
			if ($category) {
				$vpages_arr[$key]['cis'] = $this->select_vpages($dbc, $category->get_id(), 'TCMS_VPT_Article_Page_WP', $items_per_page);
				$this->prepare_for_export_articles($vpages_arr[$key]['cis']);
			}
		}
	}


	public function select_vpages(DB_Connection $dbc, $category_id, $vpt_class, $count, $offset = 0) {
		tangra_if_not_int_throw_e($category_id);
		$vpt_class = addslashes($vpt_class);

		$sql = "select vpages.id, vpages.path ".
						"from tcms_vpages vpages, ".
								"tcms_vpage_templates vpt, ".
								"tcms_vpage_categories vp_cats, ".
								"tcms_vpage_cis vp_cis, ".
								"tcms_cis cis ".
						"where vp_cats.category = $category_id and ".
								"vp_cats.vpage = vpages.id and ".
								"vpages.online = 1 and ".
								"vpages.template = vpt.id and ".
								"vpt.wp_class_name = '$vpt_class' and ".
								"vpages.id = vp_cis.vpage and ".
								"vp_cis.ci_id =  cis.id ".
						"group by vpages.id, vpages.path ".
						"order by cis.created desc";


		$rez = $dbc->select_limit($sql, $count, $offset);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = array('vpage' => $rez_obj->ID, 'path' => $rez_obj->PATH);
		}

		return $ret;
	}


	public function select_subcat_vpages(DB_Connection $dbc, $category_id, $vpt_class, $count, $offset = 0) {
		tangra_if_not_int_throw_e($category_id);
		$vpt_class = addslashes($vpt_class);

		$sql = "select vpages.id, vpages.path ".
						"from tcms_vpages vpages, ".
								"tcms_vpage_templates vpt, ".
								"tcms_vpage_categories vp_cats, ".
								"tcms_vpage_cis vp_cis, ".
								"tcms_cis cis ".
						"where vp_cats.category = $category_id and ".
								"vp_cats.vpage = vpages.id and ".
								"vpages.online = 1 and ".
								"vpages.template = vpt.id and ".
								"vpt.wp_class_name = '$vpt_class' ".
						"group by vpages.id, vpages.path ".
						"order by vp_cats.ordering asc";
		$rez = $dbc->select_limit($sql, $count, $offset);
		$ret = array();
		while ($rez_obj = $rez->fetch_object()) {
			$ret[] = array('vpage' => $rez_obj->ID, 'path' => $rez_obj->PATH);
		}

		return $ret;
	}


	private function load_vpage_category(DB_Connection $dbc, $vpage_id) {
		$ret = false;

		$page_system_attributes = TCMS_VPage_System_Attribute_DBC::load_all_for_vpage($dbc, $vpage_id);

		if ($page_system_attributes) {
			$cat_hid = current($page_system_attributes)->get_value();
			$category = new TCMS_Category_DBC();
			$dbc = $this->get_dbc();

			if ($category->load_by_hid($dbc, $cat_hid)) {
				$ret = clone $category;
			}
		}

		return $ret;
	}


	public function get_vpage_last_modified(DB_Connection $dbc = null) {
		if (!$dbc) {
			$dbc = $this->get_dbc();
		}

		$ret = '1970-01-01 00:00:00';

		$cat_hid = current(TCMS_VPage_System_Attribute_DBC::load_all_by_sa_export_name_vpage($dbc, 'category_hid', $this->get_vpage()->get_id()));

		if ($cat_hid) {
			$category = new TCMS_Category_DBC();
			$dbc = $this->get_dbc();
			if ($category->load_by_hid($dbc, $cat_hid->get_value())) {
				$vpage = $this->get_vpage();
				$current_last_modified = $vpage->get_last_modified();

				$newer_lm = TCMS_VPage_Category_DBC::find_vpage_newer_last_modified_for_cat($dbc, $category->get_id(), $current_last_modified);
				if ($newer_lm || $category->get_last_modified() > $this->get_vpage()->get_last_modified()) {
					if ($newer_lm > $category->get_last_modified()) {
						$ret = $newer_lm;
					} else {
						$ret = $category->get_last_modified();
					}
				} else {
					$ret = $current_last_modified;
				}
			}
		}

		return $ret;
	}
}